package com.neusmart.tongrentang.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.neusmart.tongrentang.F;
import com.neusmart.tongrentang.R;
import com.neusmart.tongrentang.constants.API;
import com.neusmart.tongrentang.constants.Key;
import com.neusmart.tongrentang.dataLoadRequest.DataLoadActivity;
import com.neusmart.tongrentang.model.MParam;
import com.neusmart.tongrentang.model.User;
import com.neusmart.tongrentang.result.ResultRegister;

import cn.jpush.android.api.JPushInterface;
/**
 * 注册（下一步）
 * <p/>
 * roy
 */

public class RegisterNextActivity extends DataLoadActivity implements View.OnClickListener {

    private EditText etPwd, etConfirmPwd;
    //
    private String name, phone, captcha, pwd, confirmPwd;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_register_next;
    }

    @Override
    protected void init() {
        loadExtraData();
        initView();
        setListener();
    }

    private void loadExtraData() {
        Bundle bundle = getIntent().getExtras();
        if (bundle == null)
            return;
        name = bundle.getString(Key.NAME);
        phone = bundle.getString(Key.PHONE_NUM);
        captcha = bundle.getString(Key.CAPTCHA);
    }

    private void initView() {
        etPwd = (EditText) findViewById(R.id.register_next_et_pwd);
        etConfirmPwd = (EditText) findViewById(R.id.register_next_et_confirm_pwd);
    }

    private void setListener() {
        int[] resIds = {R.id.register_next_btn_back, R.id.register_next_btn_finish};
        for (int id : resIds)
            findViewById(id).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (getCurrentFocus() != null)//隐藏软键盘
            hideSoftInput();
        switch (v.getId()) {
            case R.id.register_next_btn_back:
                onBackPressed();
                break;
            case R.id.register_next_btn_finish:
                checkBeforeRegister();
                break;
            default:
                break;
        }
    }

    private void checkBeforeRegister() {
        pwd = etPwd.getText().toString();
        confirmPwd = etConfirmPwd.getText().toString();
        if (TextUtils.isEmpty(pwd)) {
            showToast("请输入密码");
            return;
        } else if (TextUtils.isEmpty(confirmPwd)) {
            showToast("请输入确认密码");
            return;
        } else if (!pwd.equals(confirmPwd)) {
            showToast("请确认输入的密码一致");
            return;
        }
        loadData(API.REGISTER, true);
    }

    @Override
    protected void initParams(MParam param) {
        switch (param.getApi()) {
            case REGISTER:
                param.addParam("phoneNum", phone);
                param.addParam("name", name);
                param.addParam("password", pwd);
                param.addParam("confirmPassword", confirmPwd);
                param.addParam("shortMsgCode", captcha);
                param.addParam("gender", 0);
                param.addParam("pushClientId", JPushInterface.getRegistrationID(this));
                break;
            default:
                break;
        }
    }

    @Override
    protected void disposeResult(API api, String response) {
        if (response == null)
            return;
        switch (api) {
            case REGISTER:
                ResultRegister resultRegister = fromJson(response, ResultRegister.class);
                showToast(resultRegister.getFriendlyMessage());
                if (!resultRegister.isSuccess())
                    return;
                User user = resultRegister.getData().getUserProfile();
                user.setToken(resultRegister.getData().getToken());
                F.login(user);
                finish();
                break;
            default:
                break;
        }
    }

}
